package com.aifund.admin.service.impl;

import com.aifund.admin.entity.form.user.UserListForm;
import com.aifund.admin.entity.vo.PageResult;
import com.aifund.admin.entity.vo.UserListVo;
import com.aifund.admin.enums.PageCodeEnum;
import com.aifund.admin.service.UserService;
import com.aifund.common.dao.UserDao;
import com.aifund.common.entity.AiUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Autowired
    SimpleDateFormat dayFormat;

    @Override
    public PageResult<UserListVo> page(UserListForm form) {
        Specification<AiUser> specification = new Specification<AiUser>() {
            @Override
            public Predicate toPredicate(Root<AiUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(form.getStartTime()!=null&&!form.getStartTime().isEmpty()&&form.getEndTime()!=null&&!form.getEndTime().isEmpty()){
                    Predicate p1 = null;
                    try {
                        p1 = criteriaBuilder.between(root.get("createTime"), dayFormat.parse(form.getStartTime()), dayFormat.parse(form.getEndTime()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    predicates.add(p1);
                }
                if(form.getPhone()!=null&&!form.getPhone().isEmpty()){
                    Predicate p2 = criteriaBuilder.like(root.get("phone"), "%" + form.getPhone() + "%");
                    predicates.add(p2);
                }
                if(form.getNickName() != null&&!form.getNickName().isEmpty()){
                    Predicate p3 = criteriaBuilder.like(root.get("nickName"), "%" + form.getNickName() + "%");
                    predicates.add(p3);
                }
                if(form.getVipLever() != null){
                    Predicate p4 = criteriaBuilder.equal(root.<Integer>get("vipLever"), form.getVipLever());
                    predicates.add(p4);
                }
                if(form.getIsFirstOpen()!=null){
                    Predicate p5 = criteriaBuilder.equal(root.<Boolean>get("isFirstOpen"), form.getIsFirstOpen());
                    predicates.add(p5);
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Sort sort = Sort.by(Sort.Direction.ASC,"createTime");
        Pageable pageable = PageRequest.of(form.getPageIndex()-1,form.getPageSize(),sort);
        Page<AiUser> page = userDao.findAll(specification, pageable);
        List<UserListVo> vos = new ArrayList<>();
        for(AiUser aiUser : page.getContent()){
            UserListVo vo = new UserListVo();
            BeanUtils.copyProperties(aiUser,vo);
            vos.add(vo);
        }
        PageResult<UserListVo> pageResult = new PageResult<>();
        pageResult.setCode(PageCodeEnum.SUCCESS.getCode());
        pageResult.setMsg(PageCodeEnum.SUCCESS.getMsg());
        pageResult.setCount(page.getTotalElements());
        pageResult.setData(vos);
        pageResult.setPage((form.getPageIndex()));
        pageResult.setLimit(form.getPageSize());
        return pageResult;
    }

    @Override
    public AiUser getById(Long id) {
        return userDao.getById(id);
    }

    @Override
    public void save(AiUser aiUser) {
        userDao.save(aiUser);
    }

    @Override
    public int getTodayRegister() {
        return userDao.getTodayRegister();
    }

}
